import FairyEditor = CS.FairyEditor;
var App = FairyEditor.App;
/**
 * @description 文档标签扩展-自定义
 * @author hongen.gao
 */
export class DocTabPopup extends CS.FairyGUI.PopupMenu {
    constructor() {
        super('ui://nk9ejx23gcza2g');
        this.AddItem('关闭', this.close);
        this.AddItem('关闭其他组件', this.closeOther);
        this.AddItem('关闭右侧组件', this.closeRight);
        this.AddItem('关闭所有', this.closeAll);
        this.AddItem('在库中显示', this.openResource);
    }

    public add(): void {
        const comp = App.docView.GetChildAt(0).asCom;
        const tabs = comp.GetChild('tabs').asCom;
        for (let i = 0; i < tabs.numChildren; i++) {
            const tab = tabs.GetChildAt(i).asButton;
            tab.onRightClick.AddCapture((evt: CS.FairyGUI.EventContext) => {
                for (let j = 0; j < tabs.numChildren; j++) {
                    const tabj = tabs.GetChildAt(j).asButton;
                    if (tabj == tab) {
                        tabj.selected = true;
                    } else {
                        tabj.selected = false;
                    }
                }
                this.Show(tab);
                evt.StopPropagation();
            })
        }
    }

    private close(): void {
        const comp = App.docView.GetChildAt(0).asCom;
        const tabs = comp.GetChild('tabs').asCom;
        for (let i = 0; i < tabs.numChildren; i++) {
            const tab = tabs.GetChildAt(i).asButton;
            if (tab.selected) {
                const doc = App.docView.FindDocument(tab.name);
                setTimeout(() => {
                    App.docView.QueryToCloseDocument(doc);
                }, 0);
                break;
            }
        }
    }
    private closeOther(): void {
        const comp = App.docView.GetChildAt(0).asCom;
        const tabs = comp.GetChild('tabs').asCom;
        for (let i = 0; i < tabs.numChildren; i++) {
            const tab = tabs.GetChildAt(i).asButton;
            if (tab.selected) {
                //nothing to do...
            } else {
                const doc = App.docView.FindDocument(tab.name);
                setTimeout(() => {
                    App.docView.QueryToCloseDocument(doc);
                }, 0);
            }
        }
    }
    private closeRight(): void {
        const comp = App.docView.GetChildAt(0).asCom;
        const tabs = comp.GetChild('tabs').asCom;
        let selectIdx = -1;
        for (let i = 0; i < tabs.numChildren; i++) {
            const tab = tabs.GetChildAt(i).asButton;
            if (tab.selected) {
                selectIdx = i;
                break;
            }
        }
        for (let i = selectIdx + 1; i < tabs.numChildren; i++) {
            const tab = tabs.GetChildAt(i).asButton;
            const doc = App.docView.FindDocument(tab.name);
            setTimeout(() => {
                App.docView.QueryToCloseDocument(doc);
            }, 0);
        }
    }
    
    private closeAll(): void {
        App.docView.QueryToCloseAllDocuments();
    }

    private openResource(): void {
        const comp = App.docView.GetChildAt(0).asCom;
        const tabs = comp.GetChild('tabs').asCom;
        for (let i = 0; i < tabs.numChildren; i++) {
            const tab = tabs.GetChildAt(i).asButton;
            if (tab.selected) {
                const doc = App.project.GetItemByURL(tab.name);
                App.libView.Highlight(doc, true);
                break;
            }
        }
    }
}